############################
## BLIS/Swayable: Table 2 ##
############################

# File Description: 
# Uses data/packages from O_Data.R
# Replicates Table 2 from Fabric of Repair report (2025)
# Date Last Updated: 2 May 2025

############################

# Manipulate the descriptive variables of interest into a new dataframe, subsample Indigenous respondents
df_indigenous <- df %>%
  filter(race == "Indigenous") %>%
  mutate(Enrollment = enrollment_status,
         `Hawaiian Homes Beneficiary` = case_when(Are.you.a.beneficiary.of.the.Hawaiian.Homes.Commission.Act.of.1920..as.amended. == "yes" ~ "Yes",
                                                  Are.you.a.beneficiary.of.the.Hawaiian.Homes.Commission.Act.of.1920..as.amended. == "no" ~ "No",
                                                  Are.you.a.beneficiary.of.the.Hawaiian.Homes.Commission.Act.of.1920..as.amended. == "unsure" ~ "Unsure",
                                                  TRUE ~ NA),
         Residence = case_when(Area == "alaska-native-village" ~ "Alaska Native Village",
                               Area == "city" ~ "City",
                               Area == "hawaiian-homestead-through-department-of-hawaiian-home-lands" ~ "Hawaiian Homestead",
                               Area == "in-a-rural-area" ~ "Rural",
                               Area == "otsa-oklahoma-tribal-statistical-area" ~ "Oklahoma Tribal Statistical Area",
                               Area == "pueblo" ~ "Pueblo",
                               Area == "rancheria" ~ "Rancheria",
                               Area == "small-town-next-to-native-community-e-g-reservation" ~ "Small Town By Native Community",
                               Area == "small-town-not-next-to-native-community-e-g-reservation" ~ "Small Town Not By Native Community",
                               Area == "tribal-reservation" ~ "Tribal Reservation"),
         `Provided Tribal Affiliation` = tribal.affiliation.reported,
         Identity = case_when(Ethnicity..Alaska.Native == T & Ethnicity..American.Indian.Native.American == F & Ethnicity..First.Nations..Inuit.or.Metis == F & Ethnicity..Native.Hawaiian.Kanaka.Maoli == F & Ethnicity..Another.Native..ethnic..or.racial.group.not.identified.here == F ~ "Alaska Native",
                              Ethnicity..American.Indian.Native.American == T & Ethnicity..Alaska.Native == F & Ethnicity..First.Nations..Inuit.or.Metis == F & Ethnicity..Native.Hawaiian.Kanaka.Maoli == F & Ethnicity..Another.Native..ethnic..or.racial.group.not.identified.here == F 
                              ~ "American Indian, Native American",
                              Ethnicity..First.Nations..Inuit.or.Metis == T & Ethnicity..American.Indian.Native.American == F & Ethnicity..Alaska.Native == F & Ethnicity..Native.Hawaiian.Kanaka.Maoli == F & Ethnicity..Another.Native..ethnic..or.racial.group.not.identified.here == F ~ "First Nations, Inuit, Metis",
                              Ethnicity..Native.Hawaiian.Kanaka.Maoli == T & Ethnicity..First.Nations..Inuit.or.Metis == F & Ethnicity..American.Indian.Native.American == F & Ethnicity..Alaska.Native == F & Ethnicity..Another.Native..ethnic..or.racial.group.not.identified.here == F ~ "Native Hawaiian, Kanaka Maoli",
                              TRUE ~ "Other or Multiple Indigenous Identities"))

df_indigenous_table <- df_indigenous %>%
  select(Identity, Enrollment, Residence, `Provided Tribal Affiliation`, `Hawaiian Homes Beneficiary`)

# Select functions used for the `arsenal` table
my_controls2 <- tableby.control(
  test = F,
  total = F,
  numeric.test = "kwt", cat.test = "chisq",
  numeric.stats = c("meansd"),
  cat.stats = c("countpct"),
  stats.labels = list(
    meansd = "Mean (SD)"
  )
)

# Create the table
native_table <- tableby(Identity ~ ., data = df_indigenous_table, control = my_controls2)

# To see the table in LaTeX, run this code in a .rmd or .qmd file and print to PDF/html
summary(native_table)